package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

import com.Global.Global;
import com.code.chart.AbstractDemoChart;

public class Chart_DoanhTheoQui extends AbstractDemoChart {

	private double iMaxY = 0;
	// private int iMonthNowPlus = Calendar.getInstance().get(Calendar.MONTH) +
	// 2;
	private int iMonthNow = Calendar.getInstance().get(Calendar.MONTH) + 1;
	private int iYearNowAbt = Calendar.getInstance().get(Calendar.YEAR) - 1;
	private int iYearNow = Calendar.getInstance().get(Calendar.YEAR);
	private int iQuater = (iMonthNow / 3) + 1;
	private int iDate = Calendar.getInstance().get(Calendar.DATE);

	// private int iWeekNowPlus = Calendar.getInstance().get(
	// Calendar.WEEK_OF_YEAR + 1);

	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	public Intent execute(Context context, String kho) {
		double dbAllDoanhThu[] = getDoanhThuByQuater(kho);
		String[] titles = new String[] { "Doanh thu theo tuần (VNĐ/Quí)" };
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++) {
			for (int j = 0; j <= 4; j++) {
				x.add(new double[] { j + 1 });
			}
		}
		List<double[]> values = new ArrayList<double[]>();
		values.add(dbAllDoanhThu);
		// values.add(dbCallHasSPInYear);
		int[] colors = new int[] { Color.GREEN };
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		// iMaxY = iMaxY * 12 / 10;
		setChartSettings(renderer, "", "Quí", "", 0.5, 12.5, 0, iMaxY,
				Color.GRAY, Color.LTGRAY);
		renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		// renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
		renderer.setXLabels(0);
		renderer.setYLabels(10);

		for (int i = iQuater; i < iQuater + 5; i++) {
			renderer.addXTextLabel(i - iQuater + 1, returnstrQuater(i));
		}
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.CENTER);
		renderer.setPanEnabled(true, false);
		// renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);
		renderer.setZoomButtonsVisible(true);
		return ChartFactory.getBarChartIntent(
				context,
				buildBarDataset(titles, values),
				renderer,
				Type.STACKED,
				"TK doanh thu từ Quí " + String.valueOf(iQuater) + " Năm "
						+ String.valueOf(iYearNowAbt) + " đến Quí "
						+ String.valueOf(iQuater) + " Năm "
						+ String.valueOf(iYearNow));
	}

	private double[] getDoanhThuByQuater(String kho) {
		double dbThangSLGoi[] = new double[] { 0, 0, 0, 0, 0 };
		StringBuffer sql = new StringBuffer(
				" SELECT DATEPART(QUARTER,Ngay) Qui, DATEPART(YEAR,Ngay) Nam,SUM(Thanh_Tien) Tien");
		sql.append(" FROM PhieuXuat JOIN VatTuXuat ON PhieuXuat.SoCt = VatTuXuat.SoCt ");
		sql.append(" WHERE Ngay >='" + iYearNowAbt + "/" + iMonthNow + "/"
				+ iDate + "'AND Ngay <='" + iYearNow + "/" + iMonthNow + "/"
				+ iDate + "' ");
		if (!kho.equals(""))
			sql.append(" AND  MSK COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"
					+ kho + "', ',')) ");
		sql.append(" GROUP BY DATEPART(QUARTER,Ngay),DATEPART(YEAR,Ngay) ");
		sql.append(" ORDER BY DATEPART(YEAR,Ngay),DATEPART(QUARTER,Ngay) ");

		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
		try {
			while (rs.next()) {
				int quaterIndex = Integer.parseInt(rs.getString("Qui")
						.toString());
				double iTienIndex = Global.convertMooneyByDouble(Double.parseDouble(rs
						.getString("Tien").toString()));
				dbThangSLGoi[returniQuater(quaterIndex - iQuater + 4)] = (double) iTienIndex;
				dbThangSLGoi[0] = 0;
				dbThangSLGoi[4] = 0;
				if (rs.getString("Qui").toString()
						.equals(String.valueOf(iQuater))
						&& rs.getString("Nam").toString()
								.equals(String.valueOf(iYearNowAbt)))
					dbThangSLGoi[0] =  Global.convertMooneyByDouble(Double.parseDouble(rs
							.getString("Tien").toString()));
				if (rs.getString("Qui").toString()
						.equals(String.valueOf(iQuater))
						&& rs.getString("Nam").toString()
								.equals(String.valueOf(iYearNow)))
					dbThangSLGoi[4] =  Global.convertMooneyByDouble
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					(Double.parseDouble(rs
							.getString("Tien").toString()));
				// set max Y
				if (iTienIndex > iMaxY)
					iMaxY = iTienIndex;
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dbThangSLGoi;
	}

	public String returnstrQuater(int iMonth) {
		if (iMonth == 4 || iMonth == 0)
			return String.valueOf(4);
		else
			return String.valueOf(iMonth % 4);
	}

	public int returniQuater(int iMonth) {
		if (iMonth == 4 || iMonth == 0)
			return 4;
		else
			return iMonth % 4;
	}

	public Intent execute(Context context) {
		// TODO Auto-generated method stub
		return null;
	}
}
